package org.kl.bf.report.exporter;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;

/**
 * @author Qken
 * @date 2014年6月10日
 */
public class ReportCsvExporter extends AbstractReportFileExporter {
	public void export(JasperPrint jasperPrint, OutputStream os) throws JRException {
		JRCsvExporter exporter = new JRCsvExporter();
		exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
		exporter.setExporterOutput(new SimpleWriterExporterOutput(os));
		exporter.exportReport();
	}

	@Override
	public void setHeader(HttpServletResponse response) throws JRException {
		response.setContentType("application/octet-stream");
		response.setCharacterEncoding("UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+ new String((new DateTime().toString("yyyyMMddHHmmss") + ".csv").getBytes("gb2312"), "ISO8859-1") + "\"");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
